<template>
  <div class="notifications">
      <div>
          <h2 style="display: inline-block; margin-right: 10px;">{{ notifications.title }}</h2>
          <el-tag v-if="noticesEdit(this.roleNo)" style="float: right;cursor:pointer;">
              <router-link :to="{path: 'noticesEdit', query: {id:this.noticeId}}">编辑</router-link>
          </el-tag>
      </div>
    <div class="notices-info">
      <div class="info-item">
        <strong>时间：</strong>{{ notifications.createTime }}
      </div>
        <div class="notifications-content" v-html="processedContent"></div>
    </div>
  </div>
</template>

<script>
import request from "@/util/request.js"
import {noticesEdit} from "@/util/permissionJudgment.js";

export default {
    data() {
        return {
            roleNo: [],
            notifications: {},
            noticeId: this.$route.query.id,
            processedContent: ''
        }
    },
    created() {
        this.getContent(this.noticeId)
        this.setRoleNo()
    },
    methods: {
        noticesEdit,
        setRoleNo() {
            if (JSON.parse(localStorage.getItem("user")).roleNo.includes(",")) {
                this.roleNo = JSON.parse(localStorage.getItem("user"))["roleNo"].split(',')
            } else {
                this.roleNo.push(JSON.parse(localStorage.getItem("user"))["roleNo"])
            }
        },
        getContent(id) {
            request.get("/related_notice/getById?id=" + id).then(res => {
                this.notifications = res.data.data;
                this.processedContent = this.replaceImageAndVideoStyles(this.notifications.content);
            })
        },
        replaceImageAndVideoStyles(content) {
            // 替换图片和视频标签的样式
            return content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0 auto;" ')
                .replace(/<video/gi, '<video style="max-width:100%;height:auto;display:block;margin:0 auto;" ');
        }
    }
}
</script>

<style scoped>

.notifications {
  text-align: center;
  margin: 0 auto;
  max-width: 80%;
  padding: 20px;
  border-radius: 10px;
  background: linear-gradient(45deg, #87CEFA 0%, #C0F0F1 20%, #A0E2EF 39%, #68B5EB 76%, #7CACD2 100%);
  box-shadow: 0 0 4px 2px rgba(0,0,0,.4);
}

.notifications h2 {
  margin-bottom: 20px;
  color: #333;
  font-size: 28px;
}

.notices-info {
  margin-bottom: 30px;
}

.info-item {
  margin-bottom: 10px;
  text-align: left;
}

.info-item strong {
  font-weight: bold;
}

.notifications-content {
    text-align: left;
    margin-bottom: 20px;
}

/* 为段落内的图片添加样式 */
.notifications-content p img {
    text-indent: 2em;
}

/* 为图片和视频添加样式，使其居中显示 */
.notifications-content img,
.notifications-content video {
    max-width: 100%;
    height: auto; /* 让图片高度自适应，防止拉伸变形 */
    display: block;
    margin: 0 auto; /* 居中显示 */
    margin-top: 20px;
}

ul {
  list-style: none;
  padding: 0;
}

ul li {
  margin-bottom: 5px;
}

</style>
